package net.seabookchen.controller;

import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.seabookchen.dao.BlogTagDao;
import net.seabookchen.pojo.BlogTag;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.appengine.api.datastore.EntityNotFoundException;

@Controller
public class BlogTagController extends CommonController<BlogTag> {
	private static final Logger log = Logger.getLogger(BlogTagController.class.getName());
	private BlogTagDao btDao;
	
	public BlogTagController() {
		if (btDao == null) btDao = new BlogTagDao(BlogTag.class); 
	}
	
	@RequestMapping(value ="/tag", method=RequestMethod.POST)
	public String postTag(HttpServletRequest request, HttpServletResponse resp, Model model) {
		String tagStr = request.getParameter("tag");
		BlogTag tag = new BlogTag(tagStr);
		btDao.put(tag);
		populateModel(model, tag, RequestMethod.POST + "-Tag-" + tag.getId());
		log.info("Post Tag: " + tag);
		return "restful";
	}
	
	@RequestMapping(value ="/alltags", method=RequestMethod.GET)
	public String listTags(HttpServletRequest request, HttpServletResponse resp, Model model) {
		List<BlogTag> tags =  btDao.listTags();
		model.addAttribute("action", "List All Tags");
		model.addAttribute("entities", tags);
		log.info("List All Tags size: " + tags.size());
		return "showTags";
	}
	
	@RequestMapping(value ="/tag/{id}", method=RequestMethod.GET)
	public String getTag(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		BlogTag tag = null;
		try {
			tag = btDao.get(Long.parseLong(id));
		} catch (NumberFormatException e) {
			log.severe(e.getMessage());
		} catch (EntityNotFoundException e) {
			log.severe(e.getMessage());
		}
		populateModel(model, tag, RequestMethod.GET + "-Tag-" +id);
		log.info("GET Tag: " + tag);
		return "restful";
	}
	
	@RequestMapping(value ="/tag/{id}", method=RequestMethod.PUT)
	public String putTag(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		String tagStr = request.getParameter("tag");
		BlogTag tag = null;
		try {
			tag = btDao.get(Long.parseLong(id));
			tag.setTitle(tagStr);
			btDao.put(tag);
		} catch (NumberFormatException e) {
			log.severe(e.getMessage());
		} catch (EntityNotFoundException e) {
			log.severe(e.getMessage());
		}
		populateModel(model, tag, RequestMethod.GET + "-Tag-" +id);
		log.info("Put Tag: " + tag);
		return "restful";
	}
	
	@RequestMapping(value ="/tag/{id}", method=RequestMethod.DELETE)
	public String deleteTag(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		btDao.delete(Long.parseLong(id));
		model.addAttribute("action", RequestMethod.DELETE + "-Tag-" +id);
		log.info("Delete Tag: " + id);
		return "restful";
	}
}
